<!--终端管理-->

<!DOCTYPE html>
<html>

<head>
    <%- include ../taglib/layout-table.ejs %>

</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins ">
                <div class="ibox-title">
                    <h5>查询条件</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
                    <form role="form" class="form-inline">
                        <div class="form-group">
                            <label class="control-label">时间范围：</label>
                            <!--<input type="email" placeholder="请输入用户名" id="exampleInputEmail2" class="form-control">-->
                        </div>
                        <div class="form-group">
                            <input placeholder="开始时间" class="laydate-icon form-control layer-date" id="start">
                            <!--<input type="email" placeholder="请输入用户名" id="exampleInputEmail2" class="form-control">-->
                        </div>
                        <div class="form-group">
                            <input placeholder="结束时间" class="laydate-icon form-control layer-date" id="end">

                            <!--<input type="password" placeholder="请输入密码" id="exampleInputPassword2" class="form-control">-->
                        </div>
                        <div class="input-group div-serch-input">
                            <input type="text" placeholder="请录入编码名称" class="input form-control" id="serchStr">
                            <span class="input-group-btn">
                            <button type="button" class="btn btn btn-primary" onclick="query()"> <i
                                        class="fa fa-search"></i> 搜索</button>
                        </span>
                        </div>
                        <!--<button class="btn btn-white" type="button" onclick="query()"><i class="fa fa-search"></i> 查询</button>-->
                    </form>


                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>数据列表</h5>
                    <div class="ibox-tools">

                        <!--<a class="collapse-link">-->
                        <!--<i class="fa fa-chevron-up"></i>-->
                        <!--</a>-->

                    </div>
                </div>
                <div class="ibox-content">
                    <div class="" id="">
                        <!--<button id="go_search" class="btn btn-default">搜索1</button>-->
                        <button class="btn btn-primary " type="button" onclick="add()"><i class="fa fa-check"></i>&nbsp;新增
                        </button>
                        <!--<div class="btn-group">-->
                        <!--<button type="button" class="btn btn-primary "><i class="fa fa-check"></i>其他操作</button>-->
                        <!--<button type="button" class="btn btn-primary  dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">-->
                        <!--<span class="caret"></span>-->
                        <!--<span class="sr-only"></span>-->
                        <!--</button>-->
                        <!--<ul class="dropdown-menu">-->
                        <!--<a href="#" class="list-group-item list-group-item-success">批量删除</a>-->
                        <!--&lt;!&ndash;<li role="separator" class="divider"></li>&ndash;&gt;-->
                        <!--<a href="#" class="list-group-item list-group-item-info">Cras sit amet nibh libero</a>-->
                        <!--<a href="#" class="list-group-item list-group-item-warning">Porta ac consectetur ac</a>-->
                        <!--<a href="#" class="list-group-item list-group-item-danger">Vestibulum at eros</a>-->
                        <!--</ul>-->
                        <!--</div>-->
                        <button class="btn btn-danger " type="button"><i class="fa fa-close"></i> 批量删除</button>
                        <!--data-target="#myModal"-->
                        <!--<button type="button" class="btn btn-primary" data-toggle="modal" onclick="add()">-->
                        <!--打开示例窗口-->
                        <!--</button>-->
                    </div>
                    <div>
                        <table id="example" class="table table-striped table-bordered table-hover dataTables-example">
                            <thead>
                            <tr>
                                <th>
                                    <!--<input type="checkbox" class="checkall" />-->
                                </th>
                                <th width="200px">id</th>
                                <th width="200px">车牌号1</th>
                                <th width="200px">车主姓名</th>
                                <th width="200px">颜色</th>
                                <th width="200px">SIM</th>
                                <th width="200px" class="show-detail-json">操作</th>
                            </tr>
                            </thead>
                            <tbody></tbody>

                            <!--<tfoot>-->
                            <!--<tr>-->
                            <!--<th>-->
                            <!--</th>-->
                            <!--<th width="200px">id</th>-->
                            <!--<th width="200px">车牌号1</th>-->
                            <!--<th width="200px">车主姓名</th>-->
                            <!--<th width="200px">颜色</th>-->
                            <!--<th width="200px">SIM</th>-->
                            <!--<th width="200px" class="show-detail-json">操作</th>-->
                            <!--</tr>-->
                            <!--</thead>-->
                        </table>

                    </div>
                    <div class="modal inmodal" id="myModal" tabindex="-1" role="dialog" data-width="500px"
                         data-height="400px" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content animated bounceInRight">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal">
                                        <span aria-hidden="true">&times;</span><span class="sr-only">关闭</span>
                                    </button>
                                    <!--<i class="fa fa-laptop modal-icon"></i>-->
                                    <h4 class="modal-title">新增设备</h4>
                                </div>
                                <div class="modal-body" style="max-height: 400px; ">

                                    <form class="form-horizontal m-t" id="editForm" method="get">
                                        <input id="id" name="id" type="hidden">
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">车牌号：</label>
                                            <div class="col-sm-8">
                                                <input id="baseCarNo" name="baseCarNo" class="form-control"
                                                       placeholder="车牌号必填" type="text" aria-required="true"
                                                       aria-invalid="true">
                                                <!--可以增加提示-->
                                                <!--<span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 这里写点提示的内容</span>-->
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">车辆颜色：</label>
                                            <div class="col-sm-8">
                                                <input id="baseCarNoColour" name="baseCarNoColour" class="form-control"
                                                       type="text" aria-required="true" aria-invalid="true"
                                                       class="valid">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">排量：</label>
                                            <div class="col-sm-8">
                                                <input id="baseCarOutput" name="baseCarOutput" class="form-control"
                                                       type="text" aria-required="true" aria-invalid="true"
                                                       class="error">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">设备id：</label>
                                            <div class="col-sm-8">
                                                <input id="baseTerminalId" name="baseTerminalId" class="form-control"
                                                       type="text" aria-required="true" aria-invalid="true">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label">SIM：</label>
                                            <div class="col-sm-8">
                                                <input id="baseTerminalSim" name="baseTerminalSim" class="form-control"
                                                       type="text" aria-required="true" aria-invalid="true">
                                            </div>
                                        </div>
                                        <!--<div class="form-group">-->
                                        <!--<label class="col-sm-3 control-label">下拉列表：</label>-->
                                        <!--<div class="col-sm-8">-->
                                        <!--<div class="input-group">-->
                                        <!--<input type="text" name="testcombo"  class="form-control" id="testcombo">-->
                                        <!--<div class="input-group-btn">-->
                                        <!--<button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown">-->
                                        <!--<span class="caret"></span>-->
                                        <!--</button>-->
                                        <!--<ul class="dropdown-menu dropdown-menu-right" role="menu">-->
                                        <!--</ul>-->
                                        <!--</div>-->
                                        <!--&lt;!&ndash; /btn-group &ndash;&gt;-->
                                        <!--</div>-->
                                        <!--</div>-->
                                        <!--</div>-->
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <!--data-dismiss="modal" 自动触发关闭-->
                                    <button type="button" class="btn btn-white" onclick="closeModal()">关闭</button>
                                    <button type="button" class="btn btn-primary" onclick="save()">保存</button>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>

</div>


<%- include ../taglib/layout-content.ejs %>

<script type="text/javascript">

    var table;
    var validator;
    $(document).ready(function () {
        table = $('.dataTables-example').dataTable({
            "lengthChange": false,//是否允许用户自定义显示数量
//            "searching": false,//本地搜索
//            "scrollY": "220px",//dt高度
//            "autoWidth": true,//自动宽度
            "dom": '<"top"<"clear">>rt<"bottom"ilp<"clear">>',
//            "lengthChange": false,//是否允许用户自定义显示数量
            "ajax": {
                "url": "/car/list",
                "dataSrc": "rows"
            },
            serverSide: true,
            columns: [
                {
                    "sClass": "text-center",
                    "data": "id",
                    'checkboxes': {
                        'selectRow': true,
                    },

                },
                {"sClass": "text-center", data: 'id'},
                //禁止排序
                {"sClass": "text-center", data: 'baseCarNo', "bSortable": false, "orderable": false},
                {"sClass": "text-center", data: 'userName', "bSortable": false, "orderable": false},
//                {"sClass": "text-center", data: 'baseCarNoColour'},
                {
                    "sClass": "text-center", data: function (e) {//这里给最后一列返回一个操作列表
                    //e是得到的json数组中的一个item ，可以用于控制标签的属性。
                    debugger;
                    var d = e.baseCarNoColour;
                    var result = '<a class="btn btn-warning btn-rounded" >黄色</a>'
//                    车牌号颜色[1-4]黄白蓝黑
                    if (d == 2) {
                        result = '<a class="btn btn-info btn-rounded" >白色</a>'
                    } else if (d == 3) {
                        result = '<a class="btn  btn-primary btn-rounded" >蓝色</a>'
                    } else if (d == 4) {
                        result = '<a class="btn btn-default btn-rounded" >黑色</a>'
                    }
                    return result
                }
                },
                {"sClass": "text-center", data: 'baseTerminalId'},
//            { data: 'deptId' },
                {
                    "bSortable": false,//禁止排序
                    "orderable": false,
                    "sClass": "text-center",
                    data: function (d, e, f, g) {//这里给最后一列返回一个操作列表
                        //e是得到的json数组中的一个item ，可以用于控制标签的属性。
                        var id = d.id
                        var index = g.row
                        return '' +
                            '<button class="btn btn-info btn-sm btn-select-row" type="button" onclick="edit(' + index + ')" data-toggle="modal"   ><i class="fa fa-edit"></i> <span class="bold">编辑</span>' +
                            '<button class="btn btn-danger btn-sm btn-select-row" type="button"  onclick="del(' + id + ')" ><i class="fa fa-close"></i> <span class="bold">删除</span>';

                    }
                },
            ],
            //排序
            'order': [[1, 'desc']],
            initComplete: function (setting, json) {
                //隐藏列
//                $(".hiddenCol").css("display","none");
                visiblecolumn(1, false)
            },
        });

//        // 选中行数据
//        $('#button').click(function () {
//            alert(table.DataTable().rows('.selected').data().length + ' row(s) selected');
//        });
//        $(document).on("click", ".show-detail-json", function () {//取出当前行的数据
//            debugger;
//            alert(JSON.stringify(table.DataTable().row($(this).parents("tr")).data()));
//        });
        //验证
        var cusrules = {
            rules: {
                baseCarNo: {
                    required: true,
                    minlength: 3,
                    maxlength: 20
                },
//                lastname: "required",
                baseCarNoColour: {
                    required: true,
                },
                baseCarOutput: {
                    required: true,
                    minlength: 1,
                    maxlength: 99
                },
                baseTerminalId: {
                    required: true,
//                    length: 13,
                    maxlength: 20
                },
            },
            messages: {
                baseCarNo: {
                    required: icon + "请输入车牌号",
                    minlength: icon + "车牌号必须1个字符以上",
                    maxlength: icon + "车牌号必须20个字符以下"
                },
                baseTerminalId: {
                    required: icon + "请输入设备号",
//                    length: icon + "设备长度是13"
                },

            }
        };
        validator = $("#editForm").validate(cusrules);
        //日期范围限制
        var start = {
            elem: '#start',
            format: 'YYYY-MM-DD hh:mm:ss',
            min: laydate.now(), //设定最小日期为当前日期
            max: '2999-06-16 23:59:59', //最大日期
//            start: laydate.now(),
//            start: laydate.now() + ' 00:00:00',
            istime: true,
            istoday: false,
            festival: true, //是否显示节日
            choose: function (datas) {
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
//            min: laydate.now(-1), //-1代表昨天，-2代表前天，以此类推
//            max: laydate.now(+1) //+1代表明天，+2代表后天，以此类推
        };
        var end = {
            elem: '#end',
            format: 'YYYY-MM-DD hh:mm:ss',
//            min: laydate.now(),
            max: '2999-06-16 23:59:59',
//            start: laydate.now() + ' 23:59:59',
            istime: true,
            istoday: false,
            festival: true, //是否显示节日
            choose: function (datas) {
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };
        laydate(start);
        laydate(end);
        $("#start").val(laydate.now() + ' 00:00:00');
        $("#end").val(laydate.now() + ' 23:59:59');
//        下拉列表

        var testBsSuggest = $("#test").bsSuggest({
            indexId: 2, //data.value 的第几个数据，作为input输入框的内容
            indexKey: 1, //data.value 的第几个数据，作为input输入框的内容
            data: {
                'value': [
                    {
                        'id': '0',
                        'word': 'lzw',
                        'description': 'http://lzw.me'
                    },
                    {
                        'id': '1',
                        'word': 'lzwme',
                        'description': 'http://w.lzw.me'
                    },
                    {
                        'id': '2',
                        'word': 'meizu',
                        'description': 'http://www.meizu.com'
                    },
                    {
                        'id': '3',
                        'word': 'flyme',
                        'description': 'http://flyme.meizu.com'
                    }
                ],
                'defaults': 'http://lzw.me'
            }
        });
    });
    function add() {
        debugger;
        $.ajax({
            type: "GET",
            url: "/car/create",
            cache: false,
            async: false,
            dataType: "json",
            success: function (data) {
                validator.resetForm();
                $('#editForm').clearForm(true);
                $("#myModal").modal("show");
            }
        });

    }
    function edit(index) {
        debugger;

        var data = table.DataTable().rows(index).data()
        if (data && data[0]) {
            $('#editForm').resetForm();
            $('#editForm').loadDate(data[0]);
            $("#myModal").modal("show");
        } else {
            swal("已取消", "数据错误！", "warning");
        }


    }
    function del(id) {
        swal({
                title: "您确定要删除这条信息吗",
                text: "删除后将无法恢复，请谨慎操作！",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的，我要删除！",
                cancelButtonText: "让我再考虑一下…",
                closeOnConfirm: false,
                closeOnCancel: false
            },
            function (isConfirm) {
                if (isConfirm) {
                    $.ajax({
                        type: "GET",
                        url: "/car/del",
                        cache: false,
                        async: false,
                        data: {
                            ids: id
                        },
                        dataType: "json",
                        success: function (data) {
//                            swal("删除成功！", "您已经永久删除了这条信息。", "success");
                            swal("删除成功！", data, "success");
                            query();
                        }
                    });

                } else {
                    swal("已取消", "您取消了删除操作！", "error");
                }
            });
    }
    function query() {
        debugger;
        var start = $("#start").val();
        var end = $("#end").val();
        var code = $("#serchStr").val();

        table.DataTable().search(start + "," + end + "," + code, false, false).draw();
//        visiblecolumn(0,false);
    }
    function save() {
        debugger;
        $('#editForm').ajaxSubmit({
            type: 'post', // 提交方式 get/post
            dataType: 'json',
            url: '/car/save', // 需要提交的 url
            beforeSubmit: function (formData, jqForm, options) {
                if (!$("#editForm").valid()) {
                    return false;
                }
            },
            success: function (data) {
                // data 保存提交后返回的数据，一般为 json 数据
                // 此处可对 data 作相关处理
                debugger;
                if (data.success) {
                    swal({
                        title: "系统提示",
                        text: data.msg,
                        type: "success"
                    });
                    $("#myModal").setDefForm(validator);
                    $("#myModal").res
                    $("#myModal").modal("hide");
                    query();
                } else {
                    swal({
                        title: "系统提示",
                        text: data.msg,
                        type: "error"
                    });
                }
            },

        });
    }
    function closeModal() {
        $("#myModal").setDefForm(validator);
        $("#myModal").modal("hide");
    }
    //隐藏和显示列 c:列0开始，visible=false隐藏
    function visiblecolumn(c, visible) {
        table.DataTable().column(c).visible(visible)
    }
</script>


</body>

</html>
